package comm;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.lang3.StringUtils;

public class DbUtils {
	private static DataSource dataSource = null;

	public static DataSource getDataSource() throws Exception {
		if (dataSource == null) {

			Configuration config = new PropertiesConfiguration("c:\\csuu\\db.pro");
			String driverClassName = config.getString("driverClassName");
			String url = config.getString("url");

			Properties p = new Properties();
			p.setProperty("driverClassName", driverClassName);
			p.setProperty("url", url);

			dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
		}
		return dataSource;
	}

	public static QueryRunner getQueryRunner() throws Exception {
		return new QueryRunner(DbUtils.getDataSource(), true);
	}

	public static String inString(List<String> ins) {

		return "'" + StringUtils.join(ins, "','") + "'";
	}

	public static void main(String[] args) throws Exception {
		DbUtils.getQueryRunner();
	}

}
